import { Injectable } from '@angular/core';
import {
  HttpRequest,
  HttpHandler,
  HttpEvent,
  HttpInterceptor
} from '@angular/common/http';
import { Observable } from 'rxjs';

import { Router } from "@angular/router"

@Injectable()
export class AuthInterceptor implements HttpInterceptor {

  constructor(
    private router: Router
  ) { }

  intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
    let token: string = "";
    if (!localStorage.getItem("token")) {
      this.router.navigate(["login"])
    } else {
      token = localStorage.getItem("token")
    }
    // private baseUrl = 'http://192.168.31.254:8000'
   const baseUrl = 'http://202.99.212.205:8000';
    //const baseUrl = 'http://10.144.1.53:8000';
    const authReq = request.clone({
      url: baseUrl + request.url,
      headers: request.headers.set("Authorization", token)
    })
    return next.handle(authReq);
  }
}
